<?php

/* Author: tbloc@cantho.gov.vn */
/* Created Date : 17-07-2014 10:10:51 */

class M_Tai_khoan {

//Properties
    private $TK_ID;
    private $TK_USERNAME;
    private $TK_PASSWORD;
    private $TK_NGAYTAO;
    private $TK_NGAYCAPNHAT;
    private $TK_HOATDONG;
    private $TK_AVATAR;
    private $NV_ID;

//properties set get 
    public function setTK_ID($TK_ID) {
        $this->TK_ID = $TK_ID;
    }

    public function getTK_ID() {
        return $this->TK_ID;
    }    

    public function getNV_ID() {
        return $this->TK_ID;
    }
    public function setNV_ID($NV_ID) {
        $this->NV_ID = $NV_ID;
    }

    public function getTK_AVATAR() {
        return $this->TK_AVATAR;
    }
    public function setTK_AVATAR() {
        $this->TK_AVATAR = $TK_AVATAR;
    }

    public function setTK_USERNAME($TK_USERNAME) {
        $this->TK_USERNAME = $TK_USERNAME;
    }

    public function getTK_USERNAME() {
        return $this->TK_USERNAME;
    }

    public function setTK_PASSWORD($TK_PASSWORD) {
        $this->TK_PASSWORD = $TK_PASSWORD;
    }

    public function getTK_PASSWORD() {
        return $this->TK_PASSWORD;
    }

    public function setTK_NGAYTAO($TK_NGAYTAO) {
        $this->TK_NGAYTAO = $TK_NGAYTAO;
    }

    public function getTK_NGAYTAO() {
        return $this->TK_NGAYTAO;
    }

    public function setTK_NGAYCAPNHAT($TK_NGAYCAPNHAT) {
        $this->TK_NGAYCAPNHAT = $TK_NGAYCAPNHAT;
    }

    public function getTK_NGAYCAPNHAT() {
        return $this->TK_NGAYCAPNHAT;
    }

    public function setTK_HOATDONG($TK_HOATDONG) {
        $this->TK_HOATDONG = $TK_HOATDONG;
    }

    public function getTK_HOATDONG() {
        return $this->TK_HOATDONG;
    }

//crud methods
    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `tai_khoan`(                        
			`TK_ID`, 
				`TK_USERNAME`, 
				`TK_PASSWORD`, 
				`TK_NGAYTAO`, 
				`TK_NGAYCAPNHAT`, 
				`TK_HOATDONG` 
		)
	VALUES(
    		:TK_ID, 
	    		:TK_USERNAME, 
	    		:TK_PASSWORD, 
	    		:TK_NGAYTAO, 
	    		:TK_NGAYCAPNHAT, 
	    		:TK_HOATDONG 
	    );";
            $parameters = array(
                ":TK_ID",
                ":TK_USERNAME",
                ":TK_PASSWORD",
                ":TK_NGAYTAO",
                ":TK_NGAYCAPNHAT",
                ":TK_HOATDONG"
            );
            $values = array(
                $this->getTK_ID(),
                $this->getTK_USERNAME(),
                $this->getTK_PASSWORD(),
                $this->getTK_NGAYTAO(),
                $this->getTK_NGAYCAPNHAT(),
                $this->getTK_HOATDONG()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setTK_ID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "update `tai_khoan`
	                set `is_active`=0
	                where `TK_ID` = :TK_ID;";
            $parameters = array(":TK_ID");
            $values = array($this->getTK_ID());
            $types = array(PDO::PARAM_INT); //Kiem tra lai kieu du lieu nhe
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `tai_khoan` SET 
			`TK_ID`=:TK_ID,
				`TK_USERNAME`=:TK_USERNAME,
				`TK_PASSWORD`=:TK_PASSWORD,
				`TK_NGAYTAO`=:TK_NGAYTAO,
				`TK_NGAYCAPNHAT`=:TK_NGAYCAPNHAT,
				`TK_HOATDONG`=:TK_HOATDONG
			WHERE `TK_ID` = :TK_ID;";
            $parameters = array(
                ":TK_ID",
                ":TK_USERNAME",
                ":TK_PASSWORD",
                ":TK_NGAYTAO",
                ":TK_NGAYCAPNHAT",
                ":TK_HOATDONG"
            );
            $values = array(
                $this->getTK_ID(),
                $this->getTK_USERNAME(),
                $this->getTK_PASSWORD(),
                $this->getTK_NGAYTAO(),
                $this->getTK_NGAYCAPNHAT(),
                $this->getTK_HOATDONG()
            );
            $types = array(
                PDO::PARAM_INT,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_INT
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`TK_ID`,
				`TK_USERNAME`,
				`TK_PASSWORD`,
				`TK_NGAYTAO`,
				`TK_NGAYCAPNHAT`,
				`TK_HOATDONG`
			FROM `tai_khoan` WHERE `TK_ID` = :TK_ID and `is_active`=1;";
            $parameters = array(":TK_ID");
            $values = array($this->getTK_ID());
            $types = array(PDO::PARAM_INT); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTK_ID($row->TK_ID);
                $this->setTK_USERNAME($row->TK_USERNAME);
                $this->setTK_PASSWORD($row->TK_PASSWORD);
                $this->setTK_NGAYTAO($row->TK_NGAYTAO);
                $this->setTK_NGAYCAPNHAT($row->TK_NGAYCAPNHAT);
                $this->setTK_HOATDONG($row->TK_HOATDONG);
            }
            return $this;
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`TK_ID`,
				`TK_USERNAME`,
				`TK_PASSWORD`,
				`TK_NGAYTAO`,
				`TK_NGAYCAPNHAT`,
				`TK_HOATDONG`
			FROM `tai_khoan` WHERE `is_active`=1;";
            $rows = $db->executeQuery($sql);
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new tai_khoan();
                    $obj->setTK_ID($row->TK_ID);
                    $obj->setTK_USERNAME($row->TK_USERNAME);
                    $obj->setTK_PASSWORD($row->TK_PASSWORD);
                    $obj->setTK_NGAYTAO($row->TK_NGAYTAO);
                    $obj->setTK_NGAYCAPNHAT($row->TK_NGAYCAPNHAT);
                    $obj->setTK_HOATDONG($row->TK_HOATDONG);
                    $objects[] = $obj;
                }
                return $objects;
            }
        }
    }
    public function login($db = null) {
        if ($db != null) {
            $sql = "SELECT * FROM tai_khoan WHERE TK_USERNAME=:TK_USERNAME AND TK_PASSWORD=:TK_PASSWORD;";
            $parameters = array(":TK_USERNAME",":TK_PASSWORD");
            $values = array($this->getTK_USERNAME(),$this->getTK_PASSWORD());
            $types = array(PDO::PARAM_STR,PDO::PARAM_STR); //kiem tra lai kieu du lieu nhe
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setTK_ID($row->TK_ID);
                $this->setTK_USERNAME($row->TK_USERNAME);
                $this->setTK_PASSWORD($row->TK_PASSWORD);
                $this->setTK_NGAYTAO($row->TK_NGAYTAO);
                $this->setTK_NGAYCAPNHAT($row->TK_NGAYCAPNHAT);
                $this->setTK_HOATDONG($row->TK_HOATDONG);
                $this->setTK_AVATAR($row->TK_AVATAR);
                $this->setNV_ID($row->NV_ID);
                return $this;
            }else{
                return null;
            }    
             
        }
    }

}
?>

